<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use think\Db;
use think\Hook;

/**
 * 爬虫机器人接口
 */
class SpiderRobot extends Api
{
  //  protected $noNeedLogin = ['getShopInfo','listall','send'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\api\model\DbCommon');
        
    }


    //1读取爬虫参数
    public function getSpiderConfig(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "robotUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "videoRefresh",
            "videoMaxTask",
            "videoMaxLikes",
            "videoMaxComments",
            "roomInterval",
            "roomMaxTask",
            "roomIsGetCostInfo",
            "roomIsGetCostUser",
            "roomIsGetBox",
            "roomIsCall",
            "roomIsHeat",
        ];
        //查询
        $procedure_name = 'spiderrobot_param_get';
        $list = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        $this->success('返回成功', $list);
    }

    //2设置爬虫参数
    public function setSpiderConfig(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = ["robotUser",
            "videoRefresh",
            "videoMaxTask",
            "videoMaxLikes",
            "videoMaxComments",
            "roomInterval",
            "roomMaxTask",
            "roomIsGetCostInfo",
            "roomIsGetCostUser",
            "roomIsGetBox",
            "roomIsCall",
            "roomIsHeat"];
        $out_column = [
            "res",
        ];    
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'spiderrobot_param_set';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('设置成功', $rs);
        }else{
            $this->error(__('设置失败'),$rs);
        }
    }

    //3添加/更新主播数据
    public function addLivesteamer(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "userid",
            "nickname",
            "photoURL",
            "gender",
            "age",
            "faceLevel",
            "language",
            "region",
            "followNum",
            "fansNum",
            "likesNum",
            "signature",
            "videoNum",
            "newVideoTime",
            "spiderRobotUser",
            "spiderType",
            "twitter_id",
            "twitter_name",
            "youtube_channel_id",
            "youtube_channel_title",            
        ];
        $out_column = [
            "res",
        ];    
        if(!isset($params['gender'])) $params['gender'] = 0;
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'livestreamer_add';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('添加主播成功',$rs);
        }else{
            $this->error('添加主播失败',$rs);
        }
    }
/*
    //3添加/更新主播数据(这个需要用tiktok API取其他参数)
    public function addLivesteamer2(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "nickname",
            "followNum",
            "fansNum",
            "likesNum",
            "remarks",
            "spiderRobotUser",
            "spiderType",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $this->error(__('这个需要用tiktok API取其他参数'),[]);

        $procedure_name = 'livestreamer_add';
        $rs = $this->model->mysql_procedure_execute($params,$must_param,$procedure_name);
        if($rs['res']){
            $this->success('设置成功', []);
        }else{
            $this->error(__('主播已存在，请检查-livestreamerUser'),[]);
        }
    }
*/
    //4添加/更新vipuser信息
    public function addVipUserinfo(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "robotUser",
            "livestreamer",
            "vipUser",
            "nickname",
            "photoURL",
            "gender",
            "grade",
            "language",
        ];
        $out_column = [
            "res",
        ];    
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'spiderrobot_vipuser_add';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('添加vip成功',  $rs);
        }else{
            $this->error(__('添加vip用户失败'), $rs);
        }
    }

    //5抢宝箱通知(增加/更新宝箱工程信息)
    public function addBoxTask(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "nickname",
            "boxDiamands",
            "boxLeftTime",
            "onlineNum",
            "robotUser",
        ];
        $out_column = [
            "res",
        ];    
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'spiderrobot_task_box_add';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
       // $data=['msg'=>'添加成功'];
        if($rs['res']){
            $this->success('添加宝箱任务成功', $rs);
        }else{
            $this->error('添加宝箱任务失败',$rs);
        }
    }

    //6登记抢宝箱详情信息
    public function logBoxDiamonds(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "getDiamonds",
            "robotUser"
        ];
        $out_column = [
            "res",
        ];    
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $procedure_name = 'spiderrobot_box_diamands_log';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('登记宝箱金额成功', $rs);
        }else{
            $this->error('登记宝箱金额失败',$rs);
        }
    }

    //7查询抢宝箱任务
    public function queryBoxTask(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "robotUser",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "livestreamerUser",
            "boxLeftTime",
        ];
        //查询
        $procedure_name = 'spiderrobot_task_box_query';
        $list = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        $this->success('查询宝箱成功', $list);
    }

    //8取话术库
    public function getSpeakScript(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "speakType",
            "speakIn",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "speakOut",
        ];
        //查询
        $procedure_name = 'speak_script_get';
        $list = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        $this->success('查询话术成功', $list);
    }

    //9-主播是否存在
    public function livestreamer_exist(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "nickname",
            "followNum",
            "fansNum",
            "likeNum",
            "remarks",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "res",
            "grade",
            "needlike",
            "needcomment",
        ];
        //查询
        $procedure_name = 'livestreamer_exist';
        $list = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        $this->success('查询用户成功', $list);
    }

    //10-添加用户视频地址
    public function addUserVideoAddr(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "userid",
            "livestreamerUser",
            "videoindex",
            "videocoveraddr",
            "videoplayaddr",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v])){
                $this->error(__($v.'-是必传参数'),[]);
            }
        }
        $out_column = [
            "res",
        ];
        //查询
        $procedure_name = 'add_user_video_addr';
        $rs = $this->model->mysql_procedure_execute($procedure_name,$must_param,$params,$out_column);
        if($rs['res']){
            $this->success('登记视频成功', $rs);
        }else{
            $this->error('登记视频失败',$rs);
        }
    }

}
